Skip to content

Conversation

Dibbu-cell
Copy link

Description

This PR fixes the deprecation warning for asyncio.iscoroutinefunction that will be removed in Python 3.16. The function was deprecated in Python 3.12 in favor of inspect.iscoroutinefunction.

Issues

Reminders

@Dibbu-cell Dibbu-cell requested a review from a team as a code owner October 10, 2025 18:56
Comment on lines 31 to 36
iscoroutinefunction = asyncio.iscoroutinefunction # type: ignore[assignment]

def markcoroutinefunction(func):
# type: (_F) -> _F
func._is_coroutine = asyncio.coroutines._is_coroutine # type: ignore
return func

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential bug: The fallback for markcoroutinefunction uses asyncio.coroutines._is_coroutine, which was removed in Python 3.11, causing an AttributeError.
  • Description: The compatibility shim for markcoroutinefunction provides a fallback for Python versions older than 3.12. This fallback incorrectly assumes asyncio.coroutines._is_coroutine exists. However, this attribute was removed in Python 3.11. When the SDK is used with a Django ASGI application on Python 3.11, the fallback logic is triggered, leading to an AttributeError when markcoroutinefunction is called in sentry_sdk/integrations/django/asgi.py.

  • Suggested fix: The fallback implementation for markcoroutinefunction needs to be updated to not use asyncio.coroutines._is_coroutine. A different implementation that is compatible with Python versions prior to 3.12, including 3.11, should be used.
    severity: 0.85, confidence: 0.95

Did we get this right? 👍 / 👎 to inform future reviews.

Copy link
Contributor

@alexander-alderman-webb alexander-alderman-webb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @Dibbu-cell.

I have requested some initial changes.

I had a look at other libraries and there are differences at how they detect coroutines across different Python versions. I will take a another look at the changes in sentry_sdk.

We would be in line with asgiref with your changes: https://github.com/django/asgiref/blob/f587b122af17bdba5749c30b96d2237bc1c2dfdf/asgiref/sync.py#L61-L69

Starlette does a simpler version check.
https://github.com/Kludex/starlette/blob/3912d6313730cc6004dfb4436e37dbc1a81db7c8/starlette/_utils.py#L11-L15

It's worth investigating if there could be any differences with the methods for detecting full inspect and asyncio compatibility for different Python version.

Comment on lines 92 to 93
if inspect.iscoroutinefunction(f):
if iscoroutinefunction(f):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In places we're already using inspect.iscoroutinefunction(f) it's best to avoid change. Can you revert the changes in this file?

Comment on lines 35 to 36
if asyncio.iscoroutinefunction(httpx_client.get):
if iscoroutinefunction(httpx_client.get):
Copy link
Contributor

@alexander-alderman-webb alexander-alderman-webb Oct 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to call inspect.iscoroutinefunction() directly here, since we control what runs in the tests.

except Exception:
pass

if inspect.iscoroutinefunction(f):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, no need for change where we already use inspect.iscoroutinefunction().

Comment on lines 321 to 322
if inspect.iscoroutinefunction(tool):
if iscoroutinefunction(tool):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change here should also be reverted.

@Dibbu-cell
Copy link
Author

ok, i will do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace asyncio.iscoroutinefunction

2 participants